import warnings
warnings.warn("qturboframe.public_ui.ucf_h_ui.msg_box模块已被废弃，替代模块为qturboframe.public_ui.h_ui.msg_box", DeprecationWarning)

from time import sleep

from qturboframe.lib.report.log import logger
from qturboframe.lib.ranorex.web_element.web_document import WebDocument
from qturboframe.lib.ranorex.web_element.web_element import WebElement
from qturboframe.lib.ranorex.web_element.div_tag import DivTag
from qturboframe.lib.ranorex.web_element.a_tag import ATag
from ..ucf_base_element import BaseElement


class MsgBox(BaseElement):
    '''弹出框操作类'''

    def __init__(self, idname=None, father=None, timeout=10000):
        if idname:
            super().__init__(idname, father, timeout)
            self._element = DivTag(self._element.control)
        else:
            if not father:
                raise AttributeError('未定位元素，father参数不能为None！')
            if isinstance(father, WebDocument):
                self._driver = father
                self.timeout = timeout
            elif isinstance(father, WebElement):
                self._driver = father.owner_document()
                self.timeout = timeout
            else:
                raise AttributeError('father参数类型不正确！')
            re_pop_window_class = r'v-transfer-dom'
            re_modal_class = r'h-modal-wrap'
            re_modal_hidden_class = r'h-modal-hidden'
            times = 0
            while times < timeout * 10:
                # 获取所有有re_pop_window_class属性的div
                pop_windows = [DivTag(webelement.control) for webelement in father.find_elements_by_attribute(
                    'xpath', './/div[@class~\'{}\']/div[@class~\'{}\']'.format(
                        re_pop_window_class, re_modal_class), timeout=10000)]
                if pop_windows:
                    for pop_window in reversed(pop_windows):
                        class_list = pop_window.get_attribute('class').split(' ')
                        if re_modal_hidden_class not in class_list:
                            self._element = pop_window
                            break
                    else:
                        times = times + 1000
                        if times < timeout * 10:
                            sleep(0.1)
                        continue
                    break
                else:
                    times = times + 1000
                    sleep(0.1)
                    self._element = None
            else:
                self._element = None

    def close(self):
        '''
        弹出框关闭

        :Example:
         - 例: msgbox.close()
        '''
        close_btn = ATag(self._element.find_element_by_attribute(
            'xpath', './/div[@class~\'h-modal-content\']/a[@class~\'h-modal-close\']',
            timeout=self.timeout).control)
        close_btn.secure_click()

    def get_pop_window(self):
        '''
        获取弹出框元素

        :Example:
         - 例: msgbox.get_pop_window()
        '''
        popdiv = DivTag(self._element.find_element_by_attribute(
            'xpath', './/div[@class~\'h-modal-content\']', timeout=self.timeout).control)
        pop_header = popdiv.exist(
            'xpath', './/div[@class=\'h-modal-header\']/div[@class=\'h-modal-header-inner\']',
            timeout=self.timeout)
        if pop_header and pop_header.text:
            pop_header = DivTag(pop_header.control)
            logger.info('当前操作弹出框：' + pop_header.text)
        else:
            logger.info('获取弹出框。')
        return popdiv

    def exist(self):
        '''
        判断弹出框是否存在

        :Returns:
            存在返回True，否则返回False

        :Example:
         - 例: msg.exist()
        '''
        if self._element:
            return True
        else:
            return False